package com.peakmain.baselibrary.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * author ：Peakmain
 * createTime：2020/3/2
 * mail:2726449200@qq.com
 * describe：图片压缩工具
 */
public class ImageUtils {
    static {
        System.loadLibrary("native-lib");
    }
    private ImageUtils(){

    }
    private static class Holder{
        private static  ImageUtils instance=new ImageUtils();
    }
    public static ImageUtils getInstance(){
        return Holder.instance;
    }
    private int srcWidth;
    private int srcHeight;

    public void compressImage(Bitmap bitmap, int quality, String fileName) {
        compressBitmap(bitmap, quality, fileName);
    }
    public Bitmap decodeFile(String path) {
        //先获取宽度
        BitmapFactory.Options options = new BitmapFactory.Options();
        //不加载图片内存只拿宽高
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        BitmapFactory.decodeFile(path, options);
        srcWidth = options.outWidth;
        srcHeight = options.outHeight;
        options.inSampleSize = computeSize();
        options.inJustDecodeBounds = false;
        Bitmap tagBitmap =  BitmapFactory.decodeFile(path, options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Log.e("TAG","srcWidth:"+srcWidth+",srcHeight:"+srcHeight);
        try {
            if (Checker.getInstance().isJPG(new FileInputStream(path))) {
                tagBitmap = rotatingImage(tagBitmap, Checker.getInstance().getOrientation(new FileInputStream(path)));
            }
            tagBitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tagBitmap;
    }

    private Bitmap rotatingImage(Bitmap bitmap, int angle) {
        Matrix matrix = new Matrix();

        matrix.postRotate(angle);

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    private int computeSize() {
        srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
        srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;

        int longSide = Math.max(srcWidth, srcHeight);
        int shortSide = Math.min(srcWidth, srcHeight);

        float scale = ((float) shortSide / longSide);
        if (scale <= 1 && scale > 0.5625) {
            if (longSide < 1664) {
                return 1;
            } else if (longSide < 4990) {
                return 2;
            } else if (longSide > 4990 && longSide < 10240) {
                return 4;
            } else {
                return longSide / 1280 == 0 ? 1 : longSide / 1280;
            }
        } else if (scale <= 0.5625 && scale > 0.5) {
            return longSide / 1280 == 0 ? 1 : longSide / 1280;
        } else {
            return (int) Math.ceil(longSide / (1280.0 / scale));
        }
    }

    private native static int compressBitmap(Bitmap bitmap, int quality, String fileName);
}
